package com.seventh.associationsystem.controller;

import com.seventh.associationsystem.bean.*;
import com.seventh.associationsystem.service.ApplicationService;
import com.seventh.associationsystem.service.AssociationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("application")
public class ApplicationController {

    @Autowired
    private ApplicationService applicationService;
    @Autowired
    private AssociationService associationService;

    @PostMapping("create/add")
    public HttpResult addCreateApplication(@RequestParam String name, @RequestParam String description) {
        Date now = new Date();
        CreateApplication createApplication = new CreateApplication();
        User currentUser = getCurrentUser();
        createApplication.setUser(currentUser);
        createApplication.setApplyTime(now);
        createApplication.setDescription(description);
        Association association = new Association();
        association.setCreateTime(new Date());
        association.setName(name);
        association.setStatus(AssociationStatus.UNCHECKED);
        association.setChief(currentUser);
        List<User> managers = new ArrayList<>();
        List<User> members = new ArrayList<>();
        managers.add(currentUser);
        members.add(currentUser);
        association.setManagers(managers);
        association.setMembers(members);
        createApplication.setAssociation(association);
        associationService.add(association);
        applicationService.addCreateApplication(createApplication);
        return new HttpResult(ResultCode.NO_ERROR);
    }

    @PostMapping("join/add")
    public HttpResult addJoinApplication(@RequestParam String name, @RequestParam String description) {
        Date now = new Date();
        JoinApplication joinApplication = new JoinApplication();
        User currentUser = getCurrentUser();
        joinApplication.setUser(currentUser);
        joinApplication.setApplyTime(now);
        joinApplication.setDescription(description);
        Association association = associationService.findByName(name);
        joinApplication.setAssociation(association);
        applicationService.addJoinApplication(joinApplication);
        return new HttpResult(ResultCode.NO_ERROR);
    }

    @GetMapping("create/unchecked")
    public Page<CreateApplication> getUncheckedCreateApplication(@RequestParam(value = "page", defaultValue = "0") int page,
                                                                 @RequestParam(value = "pageSize", defaultValue = "5") int pageSize,
                                                                 @RequestParam(value = "pageSortField", required = false) String pageSortField,
                                                                 @RequestParam(value = "pageSortDirection", defaultValue = "ASC") String pageSortDirection) {
        return applicationService.getCreateApplicationsPageable(associationService.getUncheckedAll(), new PageRequest(page, pageSize, getOrders(pageSortField, pageSortDirection)));
    }


    @GetMapping("create/mine")
    public Page<CreateApplication> getMyCreateApplication(@RequestParam(value = "page", defaultValue = "0") int page,
                                                          @RequestParam(value = "pageSize", defaultValue = "5") int pageSize,
                                                          @RequestParam(value = "pageSortField", required = false) String pageSortField,
                                                          @RequestParam(value = "pageSortDirection", defaultValue = "ASC") String pageSortDirection) {
        return applicationService.getCreateApplicationsPageable(getCurrentUser(), new PageRequest(page, pageSize, getOrders(pageSortField, pageSortDirection)));
    }

    @PostMapping("create/check")
    public HttpResult checkCreateApplication(@RequestParam String uid, @RequestParam boolean approve) {
        CreateApplication application = applicationService.getCreateApplication(uid);
        Association association = application.getAssociation();
        association.setStatus(approve ? AssociationStatus.CHECKED : AssociationStatus.REFUSED);
        associationService.update(association);
        return new HttpResult(ResultCode.NO_ERROR);
    }

    private User getCurrentUser() {
        return ((User) SecurityContextHolder.getContext().getAuthentication().getPrincipal());
    }

    private static Sort getOrders(String pageSortField, String pageSortDirection) {
        if (pageSortField != null)
            if (pageSortDirection.compareToIgnoreCase("desc") == 0)
                return new Sort(Sort.Direction.DESC, pageSortField);
            else
                return new Sort(Sort.Direction.ASC, pageSortField);
        else
            return new Sort(Sort.Direction.DESC, "applyTime");
    }
}
